package com.zhangzhan.base.common_model.result;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.zhangzhan.base.constant.CommonConstant;
import com.zhangzhan.base.enums.CommonExceptionEnum;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;

/**
* @ClassName: Response
* @Description: 接口返回统一定义
* @author gjt
* @date 2019年10月13日 09点41分
* @param <T>
*/

@Getter
@Setter
@ToString
public class Response<T> implements Serializable {
	/**
	 * 时间戳
	 */
	@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
	private Date responseTime =new Date();
	/**
	 * 系统code
	 */
	private long sysCode = 200;
	/**
	 *系统异常返回信息
	 */
	private String sysMessage;

	/**
	 * 业务code
	 */
	private long code = 200;

	/**
	 * 业务code
	 */
	private String extraCode ;

	/**
	 * 业务异常返回信息
	 */
	private String message;


	/**
	 * 数据体
	 */
	private T data;

	public Response(){

	}

	public Response<T> success(String message) {
		this.message = message;
		this.code = CommonConstant.SC_OK_200;
		return this;
	}


	public static <T> Response<T> ok() {
		Response<T> r = new Response<T>();
		r.setCode(CommonConstant.SC_OK_200);
		r.setMessage("成功");
		return r;
	}


	public static <T> Response<T> ok(String msg) {
		Response<T> r = new Response<T>();
		r.setCode(CommonConstant.SC_OK_200);
		r.setMessage(msg);
		return r;
	}

	public static <T> Response<T> ok(T data) {
		Response<T> r = new Response<T>();
		r.setCode(CommonConstant.SC_OK_200);
		r.setData(data);
		r.setMessage(CommonExceptionEnum.SUCESS.getMsg());
		return r;
	}

	public static <T> Response<T> error(String msg) {
		return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
	}

	public static <T> Response<T> error(int code, String msg) {
		Response<T> r = new Response<T>();
		r.setCode(code);
		r.setMessage(msg);
		return r;
	}

	public Response<T> error500(String message) {
		this.message = message;
		this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
		return this;
	}
	/**
	 * 无权限访问返回结果
	 */
	public static Response<Object> noauth(String msg) {
		return error(CommonConstant.SC_JEECG_NO_AUTHZ, msg);
	}
}
